#include "Log.h"

PNE_NAMESPACE_BEGIN

Log::~Log() {}

Log &Log::GetInstance()
{
    static Log instance;
    return instance;
}

std::ostream &Log::GetCpuTraceStream()
{
    return *m_cpuTrace;
}

std::ostream &Log::GetStream()
{
    return *m_logStream;
}

void Log::SetLogStream(std::ostream &stream)
{
    m_logStream = &stream;
}

void Log::SetCpuTraceStream(std::ostream &stream)
{
    m_cpuTrace = &stream;
}
// ?
Log &Log::SetLevel(Level level)
{
    m_logLevel = level;
    return *this;
}

Level Log::GetLogLevel()
{
    return m_logLevel;
}

TeeBuf::TeeBuf(std::streambuf *sb1, std::streambuf *sb2)
    : m_sb1(sb1), m_sb2(sb2)
{
}

int TeeBuf::Overflow(int c) // interface impl
{
    if (c == EOF)
    {
        return !EOF;
    }
    else
    {
        int const r1 = m_sb1->sputc(c);
        int const r2 = m_sb2->sputc(c);

        return r1 == EOF || r2 == EOF ? EOF : c;
    }
}

int TeeBuf::Sync() // interface impl
{
    int const r1 = m_sb1->pubsync();
    int const r2 = m_sb2->pubsync();

    return r1 == 0 && r2 == 0 ? 0 : -1;
}

TeeStream ::TeeStream(std::ostream &o1, std::ostream &o2) : std::ostream(&m_tbuf),
                                                            m_tbuf(o1.rdbuf(), o2.rdbuf())
{
}

PNE_NAMESPACE_END // namespace

    // DONE